<template>
  <div>
      <el-form :inline="true" v-show="isSearchCollapse" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
            <!-- 搜索框-->
         <el-form-item prop="workOrderNumber">
                <el-input size="small" v-model="searchForm.workOrderNumber" placeholder="工单号" clearable></el-input>
         </el-form-item>
         <el-form-item prop="equipmentCode">
                <el-input size="small" v-model="searchForm.equipmentCode" placeholder="设备编码" clearable></el-input>
         </el-form-item>
         <el-form-item prop="factory">
                <el-input size="small" v-model="searchForm.factory" placeholder="厂" clearable></el-input>
         </el-form-item>
         <el-form-item prop="area">
                <el-input size="small" v-model="searchForm.area" placeholder="区域" clearable></el-input>
         </el-form-item>
         <el-form-item prop="mainEquipment">
                <el-input size="small" v-model="searchForm.mainEquipment" placeholder="主体设备" clearable></el-input>
         </el-form-item>
         <el-form-item prop="taskSource">
                <el-input size="small" v-model="searchForm.taskSource" placeholder="任务来源" clearable></el-input>
         </el-form-item>
         <el-form-item prop="roadworkContent">
                <el-input size="small" v-model="searchForm.roadworkContent" placeholder="施工内容" clearable></el-input>
         </el-form-item>
         <el-form-item prop="unsuccessfulPeople">
                <el-input size="small" v-model="searchForm.unsuccessfulPeople" placeholder="接办人" clearable></el-input>
         </el-form-item>
         <el-form-item prop="workNumber">
                <el-input size="small" v-model="searchForm.workNumber" placeholder="工号" clearable></el-input>
         </el-form-item>
         <el-form-item prop="completeTime">
                <el-input size="small" v-model="searchForm.completeTime" placeholder="完成时间" clearable></el-input>
         </el-form-item>
         <el-form-item prop="state">
                <el-input size="small" v-model="searchForm.state" placeholder="状态" clearable></el-input>
         </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="refreshList()" size="small">查询</el-button>
            <el-button @click="resetSearch()" size="small">重置</el-button>
          </el-form-item>
      </el-form>
        <!-- 导入导出-->
      <el-form :inline="true" v-show="isImportCollapse"  class="query-form" ref="importForm">
         <el-form-item>
          <el-button type="default" @click="downloadTpl()" size="small">下载模板</el-button>
         </el-form-item>
         <el-form-item prop="loginName">
            <el-upload
              class="upload-demo"
              :action="`${this.$http.BASE_URL}/flowform/form/repairTaskDistribution/import`"
              :on-success="uploadSuccess"
               :show-file-list="true">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只允许导入“xls”或“xlsx”格式文件！</div>
            </el-upload>
        </el-form-item>
      </el-form>
      <el-row>
        <!-- <el-button v-if="hasPermission('flowform:form:repairTaskDistribution:add')" type="primary" size="small" icon="el-icon-plus" @click="add()">新建</el-button>
        <el-button v-if="hasPermission('flowform:form:repairTaskDistribution:edit')" type="warning" size="small" icon="el-icon-edit-outline" @click="edit()"
         :disabled="dataListSelections.length != 1" plain>修改</el-button>
        <el-button v-if="hasPermission('flowform:form:repairTaskDistribution:del')" type="danger"   size="small" icon="el-icon-delete" @click="del()"
                  :disabled="dataListSelections.length <= 0" plain>删除
        </el-button>
        <el-button-group class="pull-right">
            <el-button
              type="default"
              size="small"
              icon="el-icon-search"
              @click="isSearchCollapse = !isSearchCollapse, isImportCollapse=false">
            </el-button>
            <el-button v-if="hasPermission('flowform:form:repairTaskDistribution:import')" type="default" size="small" icon="el-icon-upload2" title="导入" @click="isImportCollapse = !isImportCollapse, isSearchCollapse=false"></el-button>
            <el-button v-if="hasPermission('flowform:form:repairTaskDistribution:export')" type="default" size="small" icon="el-icon-download" title="导出" @click="exportExcel()"></el-button>
            <el-button
              type="default"
              size="small"
              icon="el-icon-refresh"
              @click="refreshList">
            </el-button>
        </el-button-group> -->
      </el-row>
    <el-table
      :data="dataList"
      border
      size="medium"
      @selection-change="selectionChangeHandle"
      @sort-change="sortChangeHandle"
      v-loading="loading"
      class="table">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
    <el-table-column
        prop="workOrderNumber"
        show-overflow-tooltip
        sortable="custom"
        label="工单号">
            <template slot-scope="scope">
              <el-link  type="primary" :underline="false" v-if="hasPermission('flowform:form:repairTaskDistribution:edit')" @click="edit(scope.row.id)">{{scope.row.workOrderNumber}}</el-link>
              <el-link  type="primary" :underline="false" v-else-if="hasPermission('flowform:form:repairTaskDistribution:view')"  @click="view(scope.row.id)">{{scope.row.workOrderNumber}}</el-link>
              <span v-else>{{scope.row.workOrderNumber}}</span>
            </template>
      </el-table-column>
    <el-table-column
        prop="equipmentCode"
        show-overflow-tooltip
        sortable="custom"
        label="设备编码">
      </el-table-column>
    <el-table-column
        prop="factory"
        show-overflow-tooltip
        sortable="custom"
        label="厂">
      </el-table-column>
    <el-table-column
        prop="area"
        show-overflow-tooltip
        sortable="custom"
        label="区域">
      </el-table-column>
    <el-table-column
        prop="mainEquipment"
        show-overflow-tooltip
        sortable="custom"
        label="主体设备">
      </el-table-column>
      <el-table-column
          prop="remarks"
          show-overflow-tooltip
          sortable="custom"
          label="检修性质">
          <template slot-scope="scope">
           {{ $dictUtils.getDictLabel("repair_nature", scope.row.remarks) }}
          </template>
        </el-table-column>
    <el-table-column
        prop="taskSource"
        show-overflow-tooltip
        sortable="custom"
        label="任务来源">
        <template slot-scope="scope">
         {{ $dictUtils.getDictLabel("repair_source", scope.row.taskSource) }}
        </template>
      </el-table-column>
    <el-table-column
        prop="roadworkContent"
        show-overflow-tooltip
        sortable="custom"
        label="施工内容">
      </el-table-column>
    <el-table-column
        prop="peopleName"
        show-overflow-tooltip
        sortable="custom"
        label="接办人">
      </el-table-column>
      <el-table-column
           prop="overhaulPeople"
           show-overflow-tooltip
           sortable="custom"
           label="检修标准工时">
         </el-table-column>
      <el-table-column
          prop="repirpeople"
          show-overflow-tooltip
          sortable="custom"
          label="检修标准项目承办人数">
        </el-table-column>
<el-table-column
     prop="workTime"
     show-overflow-tooltip
     sortable="custom"
     label="工时">
   </el-table-column>
     <el-table-column
         prop="project"
         show-overflow-tooltip
         sortable="custom"
         label="项目协办人">
       </el-table-column> -->
    <el-table-column
        prop="completeTime"
        show-overflow-tooltip
        sortable="custom"
        label="完成时间">
      </el-table-column>
    <el-table-column
        prop="state"
        show-overflow-tooltip
        sortable="custom"
        label="状态">
        <template slot-scope="scope">
         {{ $dictUtils.getDictLabel("repair_state", scope.row.state) }}
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        fixed="right"
        width="200"
        label="操作">
        <template  slot-scope="scope">
          <el-button v-if="hasPermission('flowform:form:repairTaskDistribution:view')" type="text" icon="el-icon-view" size="small" @click="view(scope.row.id)">查看</el-button>
          <!-- <el-button v-if="hasPermission('flowform:form:repairTaskDistribution:edit')" type="text" icon="el-icon-edit" size="small" @click="edit(scope.row.id)">修改</el-button> -->
          <!-- <el-button v-if="hasPermission('flowform:form:repairTaskDistribution:del')" type="text"  icon="el-icon-delete" size="small" @click="del(scope.row.id)">删除</el-button> -->
          <el-button type="text" v-if="judge"  icon="el-icon-view" size="small" @click="distributionTask(scope.row)">分配</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
    <RepairTaskDistributionForm  ref="repairTaskDistributionForm" @refreshDataList="refreshList" ></RepairTaskDistributionForm>
    <ProcessList ref="ProcessList" v-show='false'></ProcessList>
    <RepairTaskDistributionProcessForm  ref="repairTaskDistributionProcessForm" @refreshDataList="refreshList" v-show='false'></RepairTaskDistributionProcessForm>
    <RepairTaskDistributionFormTwo ref="repairTaskDistributionFormTwo" @refreshDataList="refreshList" v-show='false'></RepairTaskDistributionFormTwo>
  </div>
</template>

<script>
  import RepairTaskDistributionForm from './RepairTaskDistributionForm'
  import ProcessList from '../../flowable/task/ProcessList.vue'
  import RepairTaskDistributionProcessForm from '../process/RepairTaskDistributionProcessForm'
  import RepairTaskDistributionFormTwo from './RepairTaskDistributionFormTwo'
  export default {
    data () {
      return {
        searchForm: {
          workOrderNumber: '',
          equipmentCode: '',
          factory: '',
          area: '',
          mainEquipment: '',
          taskSource: '',
          roadworkContent: '',
          unsuccessfulPeople: '',
          workNumber: '',
          completeTime: '',
          state: '',
          workTime: '',
          projectMan: '',
          remarks: '',
          overhaulPeople: '',
          repirpeople: ''
        },
        outPutForm: {
          // id: '',
          workOrderNumber: '',
          equipmentCode: '',
          factory: '',
          area: '',
          mainEquipment: '',
          taskSource: '',
          roadworkContent: '',
          unsuccessfulPeople: '',
          workNumber: '',
          completeTime: '',
          state: '',
          workTime: '',
          projectMan: '',
          remarks: '',
          testId: '',
          overhaulPeople: '',
          repirpeople: ''
        },
        row: {
          id: "Process_1604327801305:48:1ef2d1bf-2f24-11eb-85a4-00ff0962600f",
          key: "Process_1604327801305",
          name: "检修流程"
        },
        dataList: [],
        pageNo: 1,
        pageSize: 10,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        judge: true,
        isSearchCollapse: false,
        isImportCollapse: false,
        loading: false
      }
    },
    components: {
      RepairTaskDistributionForm,
      ProcessList,
      RepairTaskDistributionProcessForm,
      RepairTaskDistributionFormTwo
    },
    activated () {
      this.refreshList()
    },
    methods: {
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.$http({
          url: '/flowform/form/repairTaskDistribution/list',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'orderBy': this.orderBy,
            ...this.searchForm
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.dataList = data.page.list
            this.loading = false
          }
        })
      },
      // 分配
      distributionTask (data) {
        if (data.state !== '0') {
          this.$message(`您已经分配过了`, '提示', {
            type: 'warning'
          })
        } else {
        this.loading = true
        this.editState(data.id)
         this.outPutForm.workOrderNumber = data.workOrderNumber
         this.outPutForm.equipmentCode = data.equipmentCode
         this.outPutForm.factory = data.factory
         this.outPutForm.area = data.area
         this.outPutForm.mainEquipment = data.mainEquipment
         this.outPutForm.taskSource = data.taskSource
         this.outPutForm.roadworkContent = data.roadworkContent
         this.outPutForm.unsuccessfulPeople = data.unsuccessfulPeople
         this.outPutForm.workNumber = data.workNumber
         this.outPutForm.completeTime = data.completeTime
         this.outPutForm.state = data.state
         this.outPutForm.workTime = data.workTime
         this.outPutForm.projectMan = data.projectMan
         this.outPutForm.testId = data.id
         this.outPutForm.overhaulPeople = data.overhaulPeople
         this.outPutForm.repirpeople = data.repirpeople
        this.$refs.ProcessList.start(this.row, this.outPutForm)
        // this.$refs.editState('editState', id)
        this.loading = false
        }
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageNo = val
        this.refreshList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 状态修改
       editState (id) {
         id = id || this.dataListSelections.map(item => {
           return item.id
         })[0]
         this.$refs.repairTaskDistributionFormTwo.init('editState', id)
       },
    // 排序
      sortChangeHandle (obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },
      // 新增
      add () {
        this.$refs.repairTaskDistributionForm.init('add', '')
      },
      // 修改
      edit (id) {
        id = id || this.dataListSelections.map(item => {
          return item.id
        })[0]
        this.$refs.repairTaskDistributionForm.init('edit', id)
      },
      // 查看
      view (id) {
        this.$refs.repairTaskDistributionForm.init('view', id)
      },
      // 删除
      del (id) {
        let ids = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/flowform/form/repairTaskDistribution/delete',
            method: 'delete',
            params: {'ids': ids}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      },
      // 导入成功
      uploadSuccess (res, file) {
        if (res.success) {
          this.$message.success({dangerouslyUseHTMLString: true,
            message: res.msg})
        } else {
          this.$message.error(res.msg)
        }
      },
      // 下载模板
      downloadTpl () {
        this.$utils.download('/flowform/form/repairTaskDistribution/import/template')
      },
      exportExcel () {
        this.$utils.download('/flowform/form/repairTaskDistribution/export')
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      }
    }
  }
</script>
